#include <QtGui>

 #include "imagecomposer.h"

 static const QSize resultSize(200, 200);

 ImageComposer::ImageComposer(QImage* result, Calque* calque, QWidget* parent) :
     QDialog(parent)
 {
     sourceButton = new QToolButton;
     sourceButton->setIconSize(resultSize);
     sourceButton->setFixedSize(resultSize);

     operatorComboBox = new QComboBox;
//     addOp(QPainter::CompositionMode_SourceOver, tr("SourceOver"));
//     addOp(QPainter::CompositionMode_DestinationOver, tr("DestinationOver"));
//     addOp(QPainter::CompositionMode_Clear, tr("Clear"));
//     addOp(QPainter::CompositionMode_Source, tr("Source"));
//     addOp(QPainter::CompositionMode_Destination, tr("Destination"));
//     addOp(QPainter::CompositionMode_SourceIn, tr("SourceIn"));
//     addOp(QPainter::CompositionMode_DestinationIn, tr("DestinationIn"));
//     addOp(QPainter::CompositionMode_SourceOut, tr("SourceOut"));
//     addOp(QPainter::CompositionMode_DestinationOut, tr("DestinationOut"));
//     addOp(QPainter::CompositionMode_SourceAtop, tr("SourceAtop"));
//     addOp(QPainter::CompositionMode_DestinationAtop, tr("DestinationAtop"));
//     addOp(QPainter::CompositionMode_Xor, tr("Xor"));
     addOp(QPainter::CompositionMode_Plus, tr("Addition"));
     addOp(QPainter::CompositionMode_Difference, tr("Difference"));
     addOp(QPainter::CompositionMode_Multiply, tr("Multiplier"));
     addOp(QPainter::CompositionMode_Screen, tr("Ecran")); //
     addOp(QPainter::CompositionMode_Overlay, tr("Superposer"));
     addOp(QPainter::CompositionMode_Darken, tr("Noircir seulement"));
     addOp(QPainter::CompositionMode_Lighten, tr("Eclaircir seulement"));
     addOp(QPainter::CompositionMode_ColorDodge, tr("Eclaicir"));
     addOp(QPainter::CompositionMode_ColorBurn, tr("Assombrir"));
     addOp(QPainter::CompositionMode_HardLight, tr("Lumière dure"));
     addOp(QPainter::CompositionMode_SoftLight, tr("Lumière douce"));
     addOp(QPainter::CompositionMode_Exclusion, tr("Exclusion"));

//     addOp(QPainter::RasterOp_SourceOrDestination, tr ("test"));
//     addOp(QPainter::RasterOp_SourceAndDestination, tr ("test"));
//     addOp(QPainter::RasterOp_SourceXorDestination, tr ("test"));
//     addOp(QPainter::RasterOp_NotSourceAndNotDestination, tr ("test"));
//     addOp(QPainter::RasterOp_NotSourceOrNotDestination, tr ("test"));
//     addOp(QPainter::RasterOp_NotSourceXorDestination, tr ("test"));
//     addOp(QPainter::RasterOp_NotSource, tr ("test"));
//     addOp(QPainter::RasterOp_NotSourceAndDestination, tr ("test"));
//     addOp(QPainter::RasterOp_SourceAndNotDestination, tr ("test"));

     destinationButton = new QToolButton;
     destinationButton->setIconSize(resultSize);
     destinationButton->setFixedSize(resultSize);

     equalLabel = new QLabel(tr("="));

     resultLabel = new QLabel;
     resultLabel->setMinimumWidth(resultSize.width());
     resultLabel->setFixedSize(resultSize);

     okannuler = new QDialogButtonBox(QDialogButtonBox::Ok|QDialogButtonBox::Cancel,Qt::Horizontal, this);

     connect(sourceButton, SIGNAL(clicked()), this, SLOT(chooseSource()));
     connect(operatorComboBox, SIGNAL(activated(int)),
             this, SLOT(recalculateResult()));
     connect(destinationButton, SIGNAL(clicked()),
             this, SLOT(chooseDestination()));

     connect(okannuler, SIGNAL(accepted()), this, SLOT(accept()));
     connect(okannuler, SIGNAL(rejected()), this, SLOT(reject()));


     QGridLayout *mainLayout = new QGridLayout;
     mainLayout->addWidget(sourceButton, 0, 0, 3, 1);
     mainLayout->addWidget(operatorComboBox, 1, 1);
     mainLayout->addWidget(destinationButton, 0, 2, 3, 1);
     mainLayout->addWidget(equalLabel, 1, 3);
     mainLayout->addWidget(resultLabel, 0, 4, 3, 1);
     mainLayout->addWidget(okannuler,4,4);

     mainLayout->setSizeConstraint(QLayout::SetFixedSize);


     setLayout(mainLayout);

     res = result;


     //delete res;
     //res = new QImage(resultSize, QImage::Format_ARGB32_Premultiplied);

     loadImage(calque, &destinationImage, destinationButton);
     //loadImage(":/images/checker.png", &destinationImage, destinationButton);

     setWindowTitle(tr("Image Composition"));
 }


 void ImageComposer::chooseSource()
 {
     chooseImage(tr("Choose Source Image"), &sourceImage, sourceButton);
 }

 void ImageComposer::chooseDestination()
 {
     chooseImage(tr("Choose Destination Image"), &destinationImage,
                 destinationButton);
 }

 void ImageComposer::recalculateResult()
 {
     QPainter::CompositionMode mode = currentMode();

     QPainter painter(res);

     //fond transparent
     painter.setCompositionMode(QPainter::CompositionMode_Source);
     painter.fillRect(res->rect(), Qt::transparent);

     //le image
     painter.setCompositionMode(QPainter::CompositionMode_SourceOver);

     painter.drawImage(0, 0, destinationImage);

     //masque
     painter.setCompositionMode(mode);
     painter.drawImage(0, 0, sourceImage);

     painter.setCompositionMode(QPainter::CompositionMode_DestinationOver);
     painter.fillRect(res->rect(), Qt::white);
     painter.end();

     resultLabel->setPixmap(QPixmap::fromImage(*res));
 }

 void ImageComposer::addOp(QPainter::CompositionMode mode, const QString &name)
 {
     operatorComboBox->addItem(name, mode);
 }



 void ImageComposer::chooseImage(const QString &title, QImage *image,
                                 QToolButton *button)
 {
     QString fileName = QFileDialog::getOpenFileName(this, title, QString(), "Images (*.png *pnm *.gif *.jpg *.jpeg *.bmp)");
     if (!fileName.isEmpty())
         loadImage(fileName, image, button);
 }


 void ImageComposer::loadImage(const QString &fileName, QImage *image,
                               QToolButton *button)
 {
     image->load(fileName);

     // Scale the image to given size
//     *image = image->scaled(image->size(), Qt::KeepAspectRatio);

//     QImage fixedImage(resultSize, QImage::Format_ARGB32_Premultiplied);
//     QPainter painter(&fixedImage);
//     painter.setCompositionMode(QPainter::CompositionMode_Source);
//     painter.fillRect(fixedImage.rect(), Qt::transparent);
//     painter.setCompositionMode(QPainter::CompositionMode_SourceOver);
//     painter.drawImage(imagePos(*image), *image);
//     painter.end();
     button->setIcon(QPixmap::fromImage(*image));

//     *image = fixedImage;

     recalculateResult();
 }


 void ImageComposer::loadImage(Calque* calque, QImage *image,
                               QToolButton *button)
 {
     *image = calque->copy();

     // Scale the image to given size
     //*image = image->scaled(resultSize, Qt::KeepAspectRatio);

//     QImage fixedImage(calque->size(), QImage::Format_ARGB32_Premultiplied);
//     QPainter painter(&fixedImage);
//     painter.setCompositionMode(QPainter::CompositionMode_Source);
//     painter.fillRect(fixedImage.rect(), Qt::transparent);
//     painter.setCompositionMode(QPainter::CompositionMode_SourceOver);
//     painter.drawImage(imagePos(*calque), *calque);
//     painter.end();
     button->setIcon(QPixmap::fromImage(*image));

//     *image = fixedImage;

     recalculateResult();
 }



 QPainter::CompositionMode ImageComposer::currentMode() const
 {
     return (QPainter::CompositionMode)
            operatorComboBox->itemData(operatorComboBox->currentIndex()).toInt();
 }

 QPoint ImageComposer::imagePos(const QImage &image) const
 {
     return QPoint((resultSize.width() - image.width()) / 2,
                   (resultSize.height() - image.height()) / 2);
 }
